/**
 * 
 */
package DB;

import gui.LoaderGif;

import java.util.ArrayList;
import java.util.List;
import main.Session;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.wb.swt.SWTResourceManager;

import Utils.Concurrent.AsyncTask;
import Utils.Concurrent.LoaderRunner;
import Utils.Concurrent.ThreadTasks.GetUsersTask;
import Utils.Concurrent.ThreadTasks.UpdateUsersTask;
import Utils.UI.UIUpdater;

import datatype.User;

/**
 * @author Yuval 
 * this class responsible for the user management in the gui, add
 *         the widgets listeners and etc'
 */
public class User_managment {

	// create the object with this parameters

	// the composite that
	private Composite superComposite;
	private Button commit, refresh, returnBTN;
	private Button manageUsers;
	private Canvas loadingCNVS;

	// every time we create/get them
	private Table userManagment_userTBL;
	private List<User> users;
	private Composite subComposite;
	private List<Button> checkBTNlist;
	private List<Combo> comboList;
	private GetUsersTask getUsersTask = null;
	private LoaderGif lg;

	public User_managment(Composite superComposite, Button commit,
			Button manageUsers, Button returnBTN, Button refresh,
			Canvas loadingCNVS) {
		super();
		this.superComposite = superComposite;
		this.commit = commit;
		this.manageUsers = manageUsers;
		this.returnBTN = returnBTN;
		this.refresh = refresh;
		this.loadingCNVS = loadingCNVS;

		prepareWidgets();
	}

	private void prepareWidgets() {

		commit.addMouseListener(new MouseAdapter() {

			UpdateUsersTask updateUsersTask = null;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {

				List<Integer> usersToDelete = new ArrayList<Integer>();
				List<Integer> usersToMakeAdmin = new ArrayList<Integer>();
				List<Integer> usersToMakePlayer = new ArrayList<Integer>();
				prepareUsersListToUpdate(usersToDelete, usersToMakeAdmin,
						usersToMakePlayer);
				commit.setEnabled(false);
				refresh.setEnabled(false);
				returnBTN.setEnabled(false);
				
				if(lg == null)
					lg = new LoaderGif(loadingCNVS, 4);
				lg.showLoader();

				if (updateUsersTask != null)
					Session.pool.killAll(updateUsersTask);

				updateUsersTask = new UpdateUsersTask(Session.gui
						.getDisplay(), usersToDelete, usersToMakeAdmin,
						usersToMakePlayer, users,lg);
				Session.pool.pushUrgentTask(updateUsersTask, lg.getLoaderTask());
			}

		});
		manageUsers.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {

				Session.gui.changeScreenTo(superComposite);
				
				if(lg == null)
					lg = new LoaderGif(loadingCNVS, 4);
				lg.showLoader();
				commit.setEnabled(false);
				if (getUsersTask != null)
					Session.pool.killAll(getUsersTask);

				getUsersTask = new GetUsersTask(Session.gui.getDisplay(),
						lg);
				Session.pool.pushUrgentTask(getUsersTask, lg.getLoaderTask());

			}

		});

		refresh.addMouseListener(new MouseAdapter() {

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {

				if(lg == null)
					lg = new LoaderGif(loadingCNVS, 4);
				lg.showLoader();
				commit.setEnabled(false);
				
				if (getUsersTask != null)
					Session.pool.killAll(getUsersTask);

				getUsersTask = new GetUsersTask(Session.gui.getDisplay(),lg);
				Session.pool.pushUrgentTask(getUsersTask, lg.getLoaderTask());

			}

		});

		returnBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				
				if (getUsersTask != null){
					Session.pool.killAll(getUsersTask);
				}
				
				loadingCNVS.setVisible(false);
				
				if (subComposite != null)
					if (!subComposite.isDisposed())
						subComposite.dispose();
				
				
				Session.gui.changeScreenTo(Session.gui.getMainScreen());
			}

		});

	}

	private void prepareUsersListToUpdate(List<Integer> usersToDelete,
			List<Integer> usersToMakeAdmin, List<Integer> usersToMakePlayer) {
		int i = 0;
		for (Button check : checkBTNlist) {
			if (Session.user.getId() == users.get(i).getId())
				i++;
			if (check.getSelection()) {
				usersToDelete.add(users.get(i).getId());
			}
			i++;
		}
		i = 0;
		for (Combo c : comboList) {
			if (Session.user.getId() == users.get(i).getId())
				i++;
			User u = users.get(i);
			int status = c.getSelectionIndex() == 0 ? 1 : 0;
			if (u.getPrivil() != status) {
				u.setPrivil(status);
				if (status == 0)
					usersToMakeAdmin.add(u.getId());
				else
					usersToMakePlayer.add(u.getId());
			}
			i++;

		}

	}

	public void createTableLayout() {
		
		loadingCNVS.setVisible(false);
		commit.setEnabled(true);
		refresh.setEnabled(true);
		returnBTN.setEnabled(true);

		if (subComposite != null)
			if (!subComposite.isDisposed())
				subComposite.dispose();
		subComposite = new Composite(superComposite, SWT.NONE);
		subComposite.setBounds(30, 147, 800, 430);
		userManagment_userTBL = new Table(subComposite, SWT.BORDER
				| SWT.FULL_SELECTION);
		subComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND));
		userManagment_userTBL.setBounds(28, 23, 750, 400);
		userManagment_userTBL.setHeaderVisible(true);
		userManagment_userTBL.setLinesVisible(true);

		TableColumn tblclmnUserName = new TableColumn(userManagment_userTBL,
				SWT.NONE);
		tblclmnUserName.setWidth(85);
		tblclmnUserName.setText("User name");

		TableColumn tblclmnPrivilges = new TableColumn(userManagment_userTBL,
				SWT.NONE);
		tblclmnPrivilges.setWidth(79);
		tblclmnPrivilges.setText("Privilges");
/*
		TableColumn tblclmnRegistrationDate = new TableColumn(
				userManagment_userTBL, SWT.NONE);
		tblclmnRegistrationDate.setWidth(128);
		tblclmnRegistrationDate.setText("Registration date");
*/
		TableColumn tblclmnBestLongMod = new TableColumn(userManagment_userTBL,
				SWT.NONE);
		tblclmnBestLongMod.setWidth(154);
		tblclmnBestLongMod.setText("Best Long mode score");

		TableColumn tblclmnBestLevelMod = new TableColumn(
				userManagment_userTBL, SWT.NONE);
		tblclmnBestLevelMod.setWidth(157);
		tblclmnBestLevelMod.setText("Best Level mode score");
/*
		TableColumn tblclmnDeleteUser = new TableColumn(userManagment_userTBL,
				SWT.CENTER | SWT.CHECK);
		tblclmnDeleteUser.setWidth(91);
		tblclmnDeleteUser.setText("Delete user");
		*/
	}

	public void prepareTheTable() {

		checkBTNlist = new ArrayList<Button>();
		comboList = new ArrayList<Combo>();
		for (final User u : users) {

			TableItem ti = new TableItem(userManagment_userTBL, SWT.NONE);
			ti.setText(0, u.getUserName());

		//	ti.setText(2, "00/00/00");
			ti.setText(2, ""+u.getBestLong());
			ti.setText(3, ""+u.getBestLevel());

			if (Session.user.getId() != u.getId()) {
				final Combo privCombo = new Combo(userManagment_userTBL,
						SWT.READ_ONLY);
				comboList.add(privCombo);
				privCombo.add("player");
				privCombo.add("admin");
				privCombo.setText(u.getPrivil() == 0 ? "admin" : "player");

				TableEditor tableEditor = new TableEditor(userManagment_userTBL);
				tableEditor.grabHorizontal = true;
				tableEditor.setEditor(privCombo, ti, 1);
			/*	Button deleteUserCBTN = new Button(userManagment_userTBL,
						SWT.CHECK);
				checkBTNlist.add(deleteUserCBTN);
				tableEditor = new TableEditor(userManagment_userTBL);
				tableEditor.grabHorizontal = true;
				tableEditor.setEditor(deleteUserCBTN, ti, 5); */
			} else
				ti.setText(1, "admin");
			
		}
 
	}

	public synchronized List<User> getUsers() {
		return users;
	}

	public synchronized void setUsers(List<User> users) {
		this.users = users;
	}



}
